﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using static System.Net.Mime.MediaTypeNames;

namespace SiChuan.FormsApp.AppCode.Extensions {

    public static class StringExtension {
        public static bool IsNullOrWhiteSpace(this string text) {
            return string.IsNullOrWhiteSpace(text);
        }

        public static string StringToBase64(this string input) {
            if (input.IsNullOrWhiteSpace()) return input;
            Encoding encoding = Encoding.UTF8;
            byte[] buffer = encoding.GetBytes(input.Trim());
            var ss = Convert.ToBase64String(buffer, Base64FormattingOptions.None);
            return ss;
        }

        public static string Base64ToString(this string input) {
            if (input.IsNullOrWhiteSpace()) return input;
            Encoding encoding = Encoding.UTF8;
            byte[] buffer = Convert.FromBase64String(input);
            var ss = encoding.GetString(buffer);
            return ss;
        }


        #region Xml

        /// <summary>
        /// 将对象序列化为xml文件
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="t">对象</param>
        /// <param name="path">xml存放路径</param>
        public static void ObjectToXml<T>(T t, string path) where T : class {
            XmlSerializer formatter = new XmlSerializer(typeof(T));
            using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate)) {
                formatter.Serialize(stream, t);
            }
        }

        /// <summary>
        /// 将对象序列化为xml字符串
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="t">对象</param>
        public static string ObjectToXml<T>(T t) where T : class {
            XmlSerializer formatter = new XmlSerializer(typeof(T));
            using (MemoryStream stream = new MemoryStream()) {
                formatter.Serialize(stream, t);
                string result = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                return result;
            }
        }

        /// <summary>
        /// 将xml文件反序列化为对象
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="t">对象</param>
        /// <param name="path">xml路径</param>
        /// <returns>对象</returns>
        public static T XmlToObject<T>(T t, string path) where T : class {
            XmlSerializer formatter = new XmlSerializer(typeof(T));
            using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate)) {
                XmlReader xmlReader = new XmlTextReader(stream);
                T result = formatter.Deserialize(xmlReader) as T;
                return result;
            }
        }

        public static T XmlToObjectForString<T>(this string xmlStr, string xmlRootName = "Root") where T : class {
            T result = default(T);
            if (xmlStr.IsNullOrWhiteSpace()) return result;
            if (xmlStr.LastIndexOf("<?xml version=\"1.0\" encoding=\"utf-8\"?>") > -1) xmlStr.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
            using (StringReader sr = new StringReader(xmlStr)) {
                XmlSerializer xmlSerializer = xmlStr.IsNullOrWhiteSpace() ?
                    new XmlSerializer(typeof(T)) : new XmlSerializer(typeof(T));
                result = (T)xmlSerializer.Deserialize(sr);
            }
            return result;
        }

        #endregion
    }
}
